package com.example.music.ui.recommend;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.music.R;
import com.example.music.entity.DataBanner;
import com.example.music.entity.DataMusic;
import com.example.music.entity.RequestUp;
import com.example.music.network.Api;
import com.example.music.network.RetrofitBase;
import com.example.music.utlis.LogUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RecommendViewModel extends ViewModel {

    private MutableLiveData<List<DataMusic>> dataMusics;
    private MutableLiveData<List<DataMusic>> dataUp;
    private MutableLiveData<List<DataMusic>> dataNew;
    private MutableLiveData<List<DataMusic>> dataHeart;
    private MutableLiveData<Integer> err;
    private int i = 0;
    public RecommendViewModel() {
        list.add(String.valueOf(R.mipmap.home_1));
        list.add(String.valueOf(R.mipmap.home_2));
        list.add(String.valueOf(R.mipmap.home_3));
        list.add(String.valueOf(R.mipmap.home_4));
        list.add(String.valueOf(R.mipmap.home_5));
        list.add(String.valueOf(R.mipmap.home_6));
        dataMusics = new MutableLiveData<>();
        dataUp = new MutableLiveData<>();
        dataNew = new MutableLiveData<>();
        dataHeart = new MutableLiveData<>();
        err = new MutableLiveData<>();
        Api.bannerService service = RetrofitBase.getService(Api.bannerService.class);
        service.dataBanner().enqueue(new Callback<DataBanner<List<DataMusic>>>() {
            @Override
            public void onResponse(Call<DataBanner<List<DataMusic>>> call, Response<DataBanner<List<DataMusic>>> response) {
                dataMusics.postValue(response.body().data);
            }

            @Override
            public void onFailure(Call<DataBanner<List<DataMusic>>> call, Throwable t) {
                err.postValue(i++);
            }
        });
        service.dataUP(new RequestUp("飙升榜")).enqueue(new Callback<DataBanner<List<DataMusic>>>() {
            @Override
            public void onResponse(Call<DataBanner<List<DataMusic>>> call, Response<DataBanner<List<DataMusic>>> response) {
                dataUp.postValue(response.body().data);
            }

            @Override
            public void onFailure(Call<DataBanner<List<DataMusic>>> call, Throwable t) {
                err.postValue(i++);
            }
        });
        service.dataUP(new RequestUp("新歌榜")).enqueue(new Callback<DataBanner<List<DataMusic>>>() {
            @Override
            public void onResponse(Call<DataBanner<List<DataMusic>>> call, Response<DataBanner<List<DataMusic>>> response) {
                dataNew.postValue(response.body().data);
            }

            @Override
            public void onFailure(Call<DataBanner<List<DataMusic>>> call, Throwable t) {
                err.postValue(i++);
            }
        });
        service.dataUP(new RequestUp("热歌榜")).enqueue(new Callback<DataBanner<List<DataMusic>>>() {
            @Override
            public void onResponse(Call<DataBanner<List<DataMusic>>> call, Response<DataBanner<List<DataMusic>>> response) {
                dataHeart.postValue(response.body().data);
            }

            @Override
            public void onFailure(Call<DataBanner<List<DataMusic>>> call, Throwable t) {
                err.postValue(i++);
            }
        });
    }
    private List<String> list = new ArrayList<>();

    public LiveData<List<DataMusic>> getDataBanner() {
        return dataMusics;
    }

    public List<String> getList() {
        return list;
    }
    public LiveData<List<DataMusic>> getDataUP() {
        return dataUp;
    }
    public LiveData<Integer> getErr() {
        return err;
    }
    public LiveData<List<DataMusic>> getDataNew() {
        return dataNew;
    }
    public LiveData<List<DataMusic>> getDataHeart() {
        return dataHeart;
    }
}